package baiyuanwei.example.calendar;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by baiyuanwei on 16/5/3.
 */
public class CalendarUtil {



    /**
     * 获取当月的 天数
     * */
    public static int getCurrentMonthDay() {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 现在的年
     * @return
     */
    public static int getNowYear(){
        Calendar calendar = Calendar.getInstance();
        int year =  calendar.get(Calendar.YEAR);
        return year;
    }

    /**
     * 现在的月
     * @return
     */
    public static int getNowMonth(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH)+1;
    }

    /**
     * 现在的日
     * @return
     */
    public static int getNowDay(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 根据年 月 获取对应的月份 天数
     *
     */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据日期 找到对应日期的 星期
     * @param date 格式: yyyy-MM-dd
     * @return
     */
    public static String getDayOfWeekByDate(String date) {
        String dayOfWeek = "-1";
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date myDate = myFormatter.parse(date);
            SimpleDateFormat formatter = new SimpleDateFormat("E");
            String str = formatter.format(myDate);
            dayOfWeek = str;

        } catch (Exception e) {
            System.out.println("错误!");
        }
        return dayOfWeek;
    }

//    public static String getLastMonth(String currentDate){
//
//        try {
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }

    /**
     * 将时间转化成毫秒
     * 时间格式: yyyy-MM-dd HH:mm:ss
     *
     * @param time
     * @return
     */
    public static Long timeStrToSecond(String time) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Long second = format.parse(time).getTime();
            return second;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1l;
    }
}
